//pageConditions
var pageConditions = "";

var default_pageSize = "100";


function pagination(containerid, pageSizeVal, nowPage, totalNum, method, allowScrollLoading, divPagerid, message) {
	if (nowPage == 1) {
		$("#" + divPagerid).scrollTop(0);
		localStorage.setItem(containerid, new Date().getTime());
	}
	if (allowScrollLoading == undefined || allowScrollLoading == "" || allowScrollLoading == null) {
		allowScrollLoading = "off";
	}
	var parr = (pageSizeVal + "").split("_");
	var pageSize = parseInt(parr[0]);
	nowPage = parseInt(nowPage);
	totalNum = parseInt(totalNum);
	//获取分页控件父节点
	var container = $("#" + containerid);
	//分页控件html
	var pageContent = "";
	if (totalNum > 0 && totalNum) {
		$("#" + divPagerid).css("display", "");
		//总页数
		var totalPage = pageSize == "100000000" ? 1 : (totalNum % pageSize == 0 ? totalNum / pageSize : parseInt(totalNum / pageSize) + 1);
		//记录从
		var recFrom = "";
		//记录到
		var recTo = "";

		recFrom = (nowPage - 1) * pageSize + 1;
		if (nowPage == totalPage) {
			recTo = totalNum;
		} else {
			recTo = nowPage * pageSize;
		}

		pageContent += "<div class='dataTables_paginate paging_simple_numbers' id='sample-table-2_paginate_" + containerid + "' style='position:relative;'>";
		pageContent += "<div id='div_page_loading_" + containerid + "' style='z-index:19900516;position:absolute;left:0;top:-2px;width:100%;text-align:center;border-radius:5px;display:none;'>" +
			"<img src='/img/icon/pageloading.gif' style='width:28px;height:28px;'></img>" +
			"</div>";
		pageContent += "<input type='hidden' id='endflag_" + containerid + "' value='0' >";
		pageContent += "<div id='currNum_" + containerid + "' style='display:inline-block;color: #428bca;float: left;'>" + ($("#span_page_txt_" + containerid).html() == "第" && nowPage > 1 ? $("#currNum_" + containerid).html() : "") + "</div>";
		pageContent += "<div id='selectNum_" + containerid + "' style='display:inline-block;color: green;float: left;'>" + ($("#span_page_txt_" + containerid).html() == "第" && nowPage > 1 ? $("#selectNum_" + containerid).html() : "") + "</div>";
		pageContent += "<div style='display:inline-block;'>";
		pageContent += "<ul id='pagination_ul_" + containerid + "' class='pagination' style='position:relative;'>";
		pageContent += "<li id='li_page_" + (nowPage - 1) + "' class='paginate_button previous pagebarhand' aria-controls='sample-table-2' tabindex='0' name='sample-table-2_previous' onclick='turntopage(this," + method + ",\"" + divPagerid + "\",\"" + containerid + "\");return false;'>";
		pageContent += "<span style='padding:3px 7px;'>&lt;上一页</span>";
		pageContent += "</li>";


		if (totalPage <= 7) {
			pageContent += pageCode(1, totalPage, method, divPagerid, containerid);
		} else {
			if (nowPage - 2 <= 3 && nowPage + 2 >= totalPage - 2) {
				pageContent += pageCode(1, totalPage, method, divPagerid, containerid);
			} else if (nowPage <= 3 && nowPage + 2 < totalPage - 2) {
				pageContent += pageCode(1, 5, method, divPagerid, containerid);
				pageContent += noCode();
				pageContent += pageCode(totalPage - 1, totalPage, method, divPagerid, containerid);
			} else if (nowPage - 2 <= 3 && nowPage + 2 < totalPage - 2) {
				pageContent += pageCode(1, nowPage + 2, method, divPagerid, containerid);
				pageContent += noCode();
				pageContent += pageCode(totalPage - 1, totalPage, method, divPagerid, containerid);
			} else if (nowPage - 2 > 3 && nowPage >= totalPage - 1) {
				pageContent += pageCode(1, 2, method, divPagerid, containerid);
				pageContent += noCode();
				pageContent += pageCode(totalPage - 4, totalPage, method, divPagerid, containerid);
			} else if (nowPage - 2 > 3 && nowPage + 2 >= totalPage - 2) {
				pageContent += pageCode(1, 2, method, divPagerid, containerid);
				pageContent += noCode();
				pageContent += pageCode(nowPage - 2, totalPage, method, divPagerid, containerid);
			} else {
				pageContent += pageCode(1, 2, method, divPagerid, containerid);
				pageContent += noCode();
				pageContent += pageCode(nowPage - 2, nowPage + 2, method, divPagerid, containerid);
				pageContent += noCode();
				pageContent += pageCode(totalPage - 1, totalPage, method, divPagerid, containerid);
			}
		}

		pageContent += "<li id='li_page_" + (nowPage + 1) + "' class='paginate_button next pagebarhand' aria-controls='sample-table-2' tabindex='0' name='sample-table-2_next' onclick='turntopage(this," + method + ",\"" + divPagerid + "\",\"" + containerid + "\");return false;'>";
		pageContent += "<span style='padding:3px 7px;'>下一页&gt;</span>";
		pageContent += "</li>";
		pageContent += "</ul>&nbsp;</div>";
		pageContent += "<div style='display:inline-block;vertical-align: top;position:relative;font-size:14px;color:#999999;'>" +
			"<span id='span_page_txt_" + containerid + "'>转到</span><span style='position:relative;width:42px;height:26px;'>" +
			"<input id='turnpageto_" + containerid + "' type='text' value='" + nowPage + "' onfocus='turnpagetoFocus(\"" + containerid + "\");' onblur='turnpagetoBlur(\"" + containerid + "\");' style='position:relative;height:26px;width:40px;display:inline-block;line-height:100% !important;font-size:12px;'/>" +
			"<input type='button' value='确定' id='btn_turnpageto_" + containerid + "' class='btn btn-primary btn-xs' onclick='turntopage(this," + method + ",\"" + divPagerid + "\",\"" + containerid + "\");' style='position:absolute;left:42px;display:none;'/>" +
			"</span>页 | " +
			"共<span id='totalPage_show_" + containerid + "'>" + totalPage + "</span>页 | ";

		var pSizeSelectWidth = "40px";
		if (allowScrollLoading == "on") pSizeSelectWidth = "100px";
		pageContent += "每页显示<select id='pagination_pageSize_" + containerid + "' value='' onchange='turntopage(this," + method + ",\"" + divPagerid + "\",\"" + containerid + "\"); $(\"#" + divPagerid + "\").scrollTop(0);' style='position:relative;height:26px;width:" + pSizeSelectWidth + ";line-height:100% !important;padding:0;font-size:12px;'>";

		if (allowScrollLoading == "on") pageContent += "<option id='page_sl_" + containerid + "' value='30_sl' >滚动加载(30)</option><option id='page_sl_" + containerid + "' value='100_sl' >滚动加载(100)</option><option id='page_sl_" + containerid + "' value='300_sl' >滚动加载(300)</option><option id='page_sl_" + containerid + "' value='500_sl' >滚动加载(500)</option><option id='page_sl_" + containerid + "' value='100000000_sl' >全部</option>";
		pageContent += "<option value='10'>10</option><option value='20'>20</option><option value='30'>30</option><option value='50'>50</option></select>条| " +
			"当前显示第";

		if (totalNum == 1) {
			pageContent += recFrom;
		} else {
			var pagebarType = $("#pagination_pageSize_" + containerid);
			pageContent += ((pagebarType.length > 0 && pagebarType.val().indexOf("_sl") != -1) || (pagebarType.length == 0 && allowScrollLoading == "on") ? "1" : recFrom) + "~" + (recTo == "100000000" ? totalNum : recTo);
		}
		pageContent += "条，共" + totalNum + "条</div>";
		pageContent += "</div>";
		//填充控件
		$(container).html(pageContent);

		//设置滚动加载选中
		if ($("#pagination_pageSize_" + containerid).find("option[value=" + pageSizeVal + "]").length != 1) {
			$("#page_sl_" + containerid).val(pageSizeVal);
			if ($("#page_sl_" + containerid).find("option[value=" + pageSizeVal + "]").size() > 0) {
				$("#page_sl_" + containerid).val(pageSizeVal);
			} else {
				$("<option selected value='" + pageSizeVal + "'>" + pageSizeVal + "</option>").prependTo($("#page_sl_" + containerid));
			}
		}
		if ($("#pagination_pageSize_" + containerid).find("option[value=" + pageSizeVal + "]").size() > 0) {
			$("#pagination_pageSize_" + containerid).val(pageSizeVal);
		} else {
			$("<option selected value='" + pageSizeVal + "'>" + pageSizeVal + "</option>").prependTo($("#pagination_pageSize_" + containerid));
		}


		//设置滚动加载方法
		if ((pageSizeVal + "").indexOf("_sl") != -1 && nowPage == 1) {
			/*$(window).scroll(function() {
			 var scrollTH = $(window).scrollTop();
			 var winH = $(window).height();
			 var docH = $(document).height();

			 if(docH-winH==scrollTH){
			 var turnpagetoObj = $("#btn_turnpageto");
			 turntopage(turnpagetoObj[0],method);
			 }
			 });*/
			$("#" + divPagerid).unbind("scroll").scroll(function () {
				var scrollHeightD = this.scrollHeight;
				var scrollTopD = this.scrollTop;
				var clientHeightD = this.clientHeight;
				if (scrollTopD != 0) {
					if ((scrollHeightD - scrollTopD == clientHeightD || scrollHeightD - scrollTopD < clientHeightD + 1) && new Date().getTime() - parseInt(localStorage.getItem(containerid)) > 500) {
						localStorage.setItem(containerid, new Date().getTime());
						var turnpagetoObj = $("#btn_turnpageto_" + containerid);
						turntopage(turnpagetoObj[0], method, divPagerid, containerid);
					}
				}
			});
		} else if ((pageSizeVal + "").indexOf("_sl") == 1) {
			$("#" + divPagerid).unbind('scroll');
		}

		setPageBarStatus(nowPage, totalPage, containerid);
	} else {
		//pageContent += 	"<div style='text-align:right;color:#CC0017;font-size:14px;'>未查询到相关记录集！</div>";
		if (message == "" || message == null || message == undefined) {
			message = "<div style='text-align: center;'><embed src='/img/null.svg' type='image/svg+xml'/><br><font>暂时木有内容呀~~</font></div>";
		} else {
			message = "<div style='text-align: center;'><embed src='/img/null.svg' type='image/svg+xml'/><br><font>" + message + "</font></div>";
		}
		$("#" + divPagerid).css("display", "none");
		//填充控件
		$(container).html(message);
	}

	$("#" + divPagerid + " table").on("click", function () {
		var xz = $("#" + divPagerid + " table thead th:eq(0) input:checkbox");
		if (xz.length > 0) {
			setTimeout(function () {
				var trs = $("#" + divPagerid + " table tbody tr");
				var num = 0;
				$(trs).each(function (i, e) {
					if ($(e).find("td:eq(0) input:checkbox").is(":checked")) {
						num++;
					}
				});
				if (num > 0) {
					$("#selectNum_" + containerid).html("【已选" + num + "行】");
				} else {
					$("#selectNum_" + containerid).html("");
				}
			}, 300);
		}
	});

	$("#" + divPagerid + " tbody").on("click", function (e) {
		$("#" + divPagerid + " tbody .highlight").removeClass("highlight");
		$(e.target).closest('tr').addClass('highlight');
		var currTr = $(e.target).closest('tr').index() + 1;
		$("#currNum_" + containerid).html("当前行:" + currTr + "");
	});

}

//拼装页码
function pageCode(from, to, method, divPagerid, containerid) {
	var content = "";
	for (var i = from; i <= to; i++) {
		content += "<li id='li_page_" + i + "' class='paginate_button pagebarhand' aria-controls='sample-table-2' tabindex='0' onclick='turntopage(this," + method + ",\"" + divPagerid + "\",\"" + containerid + "\");return false;'>";
		content += "<span style='padding:3px 7px;'>" + i + "</span>";
		content += "</li>";
	}

	return content;
}

//拼接省略页码
function noCode() {
	var content = "";
	content += "<li class='paginate_button disabled' aria-controls='sample-table-2' tabindex='0'>";
	content += "<span style='padding:3px 7px;'>...</span>";
	content += "</li>";

	return content;
}

//跳页输入框得到失去焦点事件
function turnpagetoFocus(containerid) {
	$("#btn_turnpageto_" + containerid).css("display", "inline-block");
	$("#btn_turnpageto_" + containerid).css("left", "40px");
}

function turnpagetoBlur(containerid) {
	setTimeout(function () {
		$("#btn_turnpageto_" + containerid).css("display", "none");
		$("#btn_turnpageto_" + containerid).css("left", "0px");
	}, 500);
}

//跳页
function turntopage(obj, getDate, divPagerid, containerid) {
	if (typeof (divPagerid) == 'object') {
		divPagerid = $(divPagerid).attr("id");
	}
	if (typeof (divPagerid) == 'string') {
	}
	// var containerid = $("#" + divPagerid).next().attr("id");
	// var containerid = $("#"+divPagerid).siblings("div[id^=urppagebar]").attr("id");
	var num = $("#turnpageto_" + containerid).val();
	var reg = new RegExp("^[0-9]*$");
	if (num == "" || num == null) {
		urp.alert("请输入数字!");
	} else if (!reg.test(num)) {
		urp.alert("请输入数字!");
	} else {
		var clickPage = "";
		var pageSizeVal = $("#pagination_pageSize_" + containerid).val();
		if (obj.id == "btn_turnpageto_" + containerid) {
			clickPage = parseInt($("#turnpageto_" + containerid).val());
			if ((pageSizeVal + "").indexOf("_sl") != -1) {
				clickPage = clickPage + 1;
			}
		} else if (obj.id == "pagination_pageSize_" + containerid) {
			clickPage = 1;
			if ((pageSizeVal + "").indexOf("_sl") != -1) {
				//进行滚动加载
				$("#pagination_ul_" + containerid).css("display", "none");
				$("#turnpageto_" + containerid).attr("readOnly", true);
				$("#turnpageto_" + containerid).removeAttr("onfocus");
				$("#span_page_txt_" + containerid).text("第");
				$("#turnpageto_" + containerid).val("0");
				/*$(window).scroll(function() {
				 var scrollTH = $(window).scrollTop();
				 var winH = $(window).height();
				 var docH = $(document).height();

				 if(docH-winH==scrollTH){
				 var turnpagetoObj = $("#btn_turnpageto");
				 turntopage(turnpagetoObj[0],getDate);
				 }
				 });*/
				$("#" + divPagerid).unbind("scroll");
				$("#" + divPagerid).scroll(function () {
					var scrollHeightD = this.scrollHeight;
					var scrollTopD = this.scrollTop;
					var clientHeightD = this.clientHeight;
					if (scrollTopD != 0) {
						if ((scrollHeightD - scrollTopD == clientHeightD || scrollHeightD - scrollTopD < clientHeightD + 1) && new Date().getTime() - parseInt(localStorage.getItem(containerid)) > 500) {
							localStorage.setItem(containerid, new Date().getTime());
							var turnpagetoObj = $("#btn_turnpageto_" + containerid);
							turntopage(turnpagetoObj[0], getDate, divPagerid, containerid);
						}
					}
				});

			} else {
				if ($("#pagination_ul_" + containerid).css("display") == "none") {
					$("#pagination_ul_" + containerid).css("display", "inline-block");
					$("#turnpageto_" + containerid).attr("readOnly", false);
					$("#turnpageto_" + containerid).attr("onfocus", "turnpagetoFocus();");
					$("#span_page_txt_" + containerid).text("转到");
					$("#" + divPagerid).unbind('scroll');
				}
			}
		} else {
			var idarr = obj.id.split("_");
			clickPage = parseInt(idarr[2]);
		}

		var totalPage = parseInt($("#totalPage_show_" + containerid).text());
		if ((pageSizeVal + "").indexOf("_sl") != -1 && clickPage != 1 && clickPage > totalPage) {
			if ($("#endflag_" + containerid).val() == "0") {
				urp.alert("数据已加载完！");
				$("#endflag_" + containerid).val(1)
			} else {
				console.log("数据已加载完！");
			}
			return;
		}

		if (clickPage < 1) {
			clickPage = 1;
		} else if (clickPage > totalPage) {
			clickPage = totalPage;
		}

		$("#div_page_loading_" + containerid).show();
		getDate(clickPage, pageSizeVal);
	}
}

//设置分页状态
function setPageBarStatus(nowPage, totalPage, containerid) {
	$("#" + containerid + " #li_page_" + nowPage).addClass("active");
	$("#" + containerid + " #li_page_" + nowPage).removeAttr("onclick");
	if (nowPage == 1) {
		$("li[name=sample-table-2_previous]").addClass("disabled");
		$("li[name=sample-table-2_previous]").removeClass("pagebarhand");
		$("li[name=sample-table-2_previous]").removeAttr("onclick");
	}
	if (nowPage == totalPage) {
		$("li[name=sample-table-2_next]").addClass("disabled");
		$("li[name=sample-table-2_next]").removeClass("pagebarhand");
		$("li[name=sample-table-2_next]").removeAttr("onclick");
	}
	var pageSizeVal = $("#pagination_pageSize_" + containerid).val();
	if ((pageSizeVal + "").indexOf("_sl") != -1) {
		//进行滚动加载
		$("#pagination_ul_" + containerid).css("display", "none");
		$("#turnpageto_" + containerid).attr("readOnly", true);
		$("#turnpageto_" + containerid).removeAttr("onfocus");
		$("#span_page_txt_" + containerid).text("第");
	} else {
		if ($("#pagination_ul_" + containerid).css("display") == "none") {
			$("#pagination_ul_" + containerid).css("display", "inline-block");
			$("#turnpageto_" + containerid).attr("readOnly", false);
			$("#turnpageto_" + containerid).attr("onfocus", "turnpagetoFocus();");
			$("#span_page_txt_" + containerid).text("转到");
		}
	}
	if (nowPage != 1) {
		urp.alert("第 " + nowPage + " 页， 共 " + totalPage + " 页");
	}

}